Framework.js ➔ Framework   A
last analyzed

Complexity

Conditions 1
Paths 32

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 32
dl 0
loc 72
rs 9.102
nop 1

3 Functions

Rating   Name   Duplication   Size   Complexity  
A Framework.js ➔ ... ➔ this.execute 0 22 1
A Framework.js ➔ ... ➔ this.run 0 3 1
A Framework.js ➔ ... ➔ this.prepare 0 16 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* global VoxEngine */
2
3
var Slf4j = require('@ama-team/voxengine-sdk').Logger.Slf4j
4
var Barricade = require('./Barricade').Barricade
5
var Run = require('../Execution').Run
6
var Binder = require('../Adapter/Binder').Binder
7
var Printer = require('./Printer').Printer
8
var OperationStatus = require('../Schema').OperationStatus
9
10
/**
11
 * Main library class that provides public API.
12
 *
13
 * @param {TFrameworkOptions} [options]
14
 *
15
 * @class
16
 */
17
function Framework (options) {
18
  var self = this
19
  options = options || {}
20
  options.behavior = options.behavior || {terminate: true}
21
  var logger = Slf4j.factory(options.logger, 'ama-team.vsf.framework')
22
  var printer = new Printer(options.logger)
23
24
  /**
25
   * Runs provided scenario.
26
   *
27
   * @param {TScenarioInput} scenario
28
   *
29
   * @return {TRunResult}
30
   */
31
  this.run = function (scenario) {
32
    return self.execute(self.prepare(scenario))
33
  }
34
35
  /**
36
   * Executes prepared Run
37
   *
38
   * @param {Run} run
39
   *
40
   * @return {TRunResult}
41
   */
42
  this.execute = function (run) {
43
    printer.scenario(run.getScenario())
44
    run.initialize()
45
    Binder.bind(run)
46
    return run
47
      .getCompletion()
48
      .then(printer.result, function (reason) {
49
        logger.error('Unexpected error during execution:', reason)
50
        return {
51
          status: OperationStatus.Tripped,
52
          error: reason,
53
          stages: {}
54
        }
55
      })
56
      .then(function (result) {
57
        if (options.behavior.terminate) {
58
          logger.debug('Shutting down VoxEngine')
59
          VoxEngine.terminate()
60
        }
61
        return result
62
      })
63
  }
64
65
  /**
66
   * Creates run from scenario input.
67
   *
68
   * @param {TScenarioInput} scenario
69
   *
70
   * @return {Run}
71
   */
72
  this.prepare = function (scenario) {
73
    try {
74
      var barricade = new Barricade({logger: options.logger, printer: printer})
75
      var normalized = barricade.scenario(scenario)
76
      var settings = {
77
        state: scenario.state || {},
78
        arguments: scenario.arguments || {},
79
        container: scenario.container || {},
80
        logger: options.logger
81
      }
82
      return new Run(normalized, normalized.deserializer, settings)
83
    } catch (e) {
84
      logger.error('Failed to create run, most probably due to invalid scenario')
85
      throw e
86
    }
87
  }
88
}
89
90
module.exports = {
91
  Framework: Framework
92
}
93